<template>
  <div class="dify-chat">
    <!-- 简洁的页面标题 -->
    <div class="page-header">
      <h1>智能客服</h1>
    </div>

    <!-- 聊天容器 -->
    <div class="chat-container">
      <div class="chat-header">
        <div class="chat-title">
          <el-icon class="chat-icon"><ChatDotRound /></el-icon>
          <span>AI助手</span>
        </div>
        <div class="chat-status">
          <el-tag type="success" size="small" effect="plain">
            <el-icon><Connection /></el-icon>
            在线
          </el-tag>
        </div>
      </div>

      <!-- Dify聊天iframe -->
      <div class="chat-iframe-container">
        <iframe
          ref="chatIframe"
          :src="difyChatUrl"
          frameborder="0"
          allowfullscreen
          class="chat-iframe"
          @load="handleIframeLoad"
        ></iframe>
      </div>

      <!-- 加载状态 -->
      <div v-if="loading" class="loading-container">
        <el-icon class="loading-icon"><Loading /></el-icon>
        <p>连接中...</p>
      </div>

      <!-- 错误状态 -->
      <div v-if="error" class="error-container">
        <el-icon class="error-icon"><Warning /></el-icon>
        <p>连接失败</p>
        <el-button type="primary" size="small" @click="retryConnection">
          <el-icon><Refresh /></el-icon>
          重试
        </el-button>
      </div>
    </div>

    <!-- 简洁的操作栏 -->
    <div class="action-bar">
      <el-button type="primary" @click="openFullScreen" size="small">
        <el-icon><FullScreen /></el-icon>
        全屏
      </el-button>
      <el-button @click="refreshChat" size="small">
        <el-icon><Refresh /></el-icon>
        刷新
      </el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { 
  ChatDotRound, 
  Connection, 
  Loading, 
  Warning, 
  Refresh, 
  FullScreen 
} from '@element-plus/icons-vue'

// 响应式数据
const loading = ref(true)
const error = ref(false)
const chatIframe = ref<HTMLIFrameElement>()

// Dify聊天链接
const difyChatUrl = 'https://udify.app/chat/xQEnUR4gkknLco8c'

// 方法
const handleIframeLoad = () => {
  loading.value = false
  error.value = false
}

const retryConnection = () => {
  loading.value = true
  error.value = false
  
  if (chatIframe.value) {
    chatIframe.value.src = difyChatUrl
  }
}

const openFullScreen = () => {
  window.open(difyChatUrl, '_blank', 'width=1200,height=800,scrollbars=yes,resizable=yes')
  ElMessage.success('已在新窗口打开全屏聊天')
}

const refreshChat = () => {
  if (chatIframe.value) {
    chatIframe.value.src = difyChatUrl
  }
  ElMessage.success('聊天已刷新')
}

// 生命周期
onMounted(() => {
  // 设置超时检测
  setTimeout(() => {
    if (loading.value) {
      error.value = true
      loading.value = false
    }
  }, 10000) // 10秒超时
})
</script>

<style scoped>
.dify-chat {
  padding: 24px;
  max-width: 1000px;
  margin: 0 auto;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.page-header {
  margin-bottom: 24px;
  text-align: center;
}

.page-header h1 {
  margin: 0;
  color: #2c3e50;
  font-size: 32px;
  font-weight: 300;
  letter-spacing: -0.5px;
}

.chat-container {
  background: white;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  margin-bottom: 16px;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.chat-title {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 18px;
  font-weight: 500;
}

.chat-icon {
  font-size: 20px;
}

.chat-status {
  display: flex;
  align-items: center;
  gap: 6px;
}

.chat-iframe-container {
  position: relative;
  height: 650px;
  background-color: #fafbfc;
}

.chat-iframe {
  width: 100%;
  height: 100%;
  border: none;
  background-color: white;
}

.loading-container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: #8e8e93;
}

.loading-icon {
  font-size: 28px;
  color: #667eea;
  animation: rotate 2s linear infinite;
}

.error-container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: #ff6b6b;
}

.error-icon {
  font-size: 28px;
  margin-bottom: 12px;
}

.action-bar {
  display: flex;
  justify-content: center;
  gap: 12px;
  padding: 16px 0;
}

.action-bar .el-button {
  border-radius: 8px;
  font-weight: 500;
  padding: 8px 16px;
  transition: all 0.3s ease;
}

.action-bar .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

@keyframes rotate {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .dify-chat {
    padding: 16px;
  }
  
  .page-header h1 {
    font-size: 28px;
  }
  
  .chat-iframe-container {
    height: 500px;
  }
  
  .chat-header {
    padding: 16px 20px;
  }
  
  .chat-title {
    font-size: 16px;
  }
  
  .action-bar {
    flex-direction: column;
    align-items: center;
  }
  
  .action-bar .el-button {
    width: 120px;
  }
}

@media (max-width: 480px) {
  .dify-chat {
    padding: 12px;
  }
  
  .page-header h1 {
    font-size: 24px;
  }
  
  .chat-iframe-container {
    height: 400px;
  }
  
  .chat-header {
    padding: 12px 16px;
  }
  
  .chat-title {
    font-size: 14px;
  }
}

/* 按钮样式优化 */
:deep(.el-button) {
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
}

:deep(.el-button--primary) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  color: white;
}

:deep(.el-button--primary:hover) {
  background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
}

:deep(.el-button:not(.el-button--primary)) {
  background: white;
  border: 1px solid #e1e5e9;
  color: #6c757d;
}

:deep(.el-button:not(.el-button--primary):hover) {
  background: #f8f9fa;
  border-color: #667eea;
  color: #667eea;
}

/* 标签样式优化 */
:deep(.el-tag) {
  border-radius: 6px;
  font-weight: 500;
}

:deep(.el-tag--success) {
  background-color: rgba(103, 194, 58, 0.1);
  border-color: rgba(103, 194, 58, 0.2);
  color: #67c23a;
}
</style>
